package cn.app.android.sichuanxiaoke.com.xiaokeapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.bean.Commodity;

/**
 * Created by weian on 2017/8/18.
 */

public class MyGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<Commodity> commodities;
    private LayoutInflater inflater;
    public MyGridViewAdapter(Context context, List<Commodity> commodities) {
        this.context = context;
        this.commodities = commodities;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return commodities.size();
    }

    @Override
    public Object getItem(int i) {
        return commodities.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    class ViewHolder {
        private ImageView img;
        private TextView tv1,tv2;
    }
    ViewHolder holder = null;
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view==null)
        {
            view = inflater.inflate(R.layout.grid_view_item,null);
            holder = new ViewHolder();
            holder.img = (ImageView) view.findViewById(R.id.grid_img);
            holder.tv1 = (TextView) view.findViewById(R.id.grid_name);
            holder.tv2 = (TextView) view.findViewById(R.id.grid_price);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(commodities.get(i).getImg_url()).placeholder(R.mipmap.icon).
                into(holder.img);
        holder.tv1.setText(commodities.get(i).getName());
        holder.tv2.setText("现售价:"+commodities.get(i).getPrice()+".00 元");
        return view;
    }
}
